package com.yanqu.road.entity.activity.sonhai.data;

import com.yanqu.road.utils.date.DateHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 异兽类
 */
public class SonHaiBeastData {

    //异兽配置ID
    private int id;

    //总血量 注入配置的血量
    private long allBlood = 0;

    //总掉血
    private long allReBlood = 0;

    //掉血详情.K:玩家ID,V:掉血详情
    private Map<Long, SonHaiReBloodInfo> reBloodInfoMap = new ConcurrentHashMap<>();

    public SonHaiBeastData() {}

    public SonHaiBeastData(int id, long allBlood) {
        this.id = id;
        this.allBlood = allBlood;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public long getAllBlood() {
        return allBlood;
    }

    public void setAllBlood(long allBlood) {
        this.allBlood = allBlood;
    }

    public void setAllReBlood(long allReBlood) {
        this.allReBlood = allReBlood;
    }

    public void setReBloodInfoMap(Map<Long, SonHaiReBloodInfo> reBloodInfoMap) {
        this.reBloodInfoMap = reBloodInfoMap;
    }

    public Map<Long, SonHaiReBloodInfo> getReBloodInfoMap() {
        return this.reBloodInfoMap;
    }

    /**
     * 获取当前血量（真实）
     * @return
     */
    public long getBlood() {
        return Math.max(this.allBlood - this.allReBlood, 0);
    }

    /**
     * 获取小怪总掉血（真实）
     * @return
     */
    public long getAllReBlood() {
        return this.allReBlood;
    }

    /**
     * 添加异兽掉血
     * @param userId 玩家ID
     * @param reBlood 表现掉血
     * @param realReBlood 真实掉血
     */
    public synchronized void addReBlood(long userId, long reBlood, long realReBlood) {
        //总掉血
        this.addReBlood(realReBlood);
        //掉血明细
        this.addReBloodInfo(userId, reBlood);
    }

    /**
     * 掉血
     * @param realReBlood
     */
    public synchronized void addReBlood(long realReBlood) {
        this.allReBlood += realReBlood;
    }

    /**
     * 记录掉血明细
     * @param userId
     * @param reBlood
     */
    public synchronized void addReBloodInfo(long userId, long reBlood) {
        //掉血明细
        SonHaiReBloodInfo info = this.reBloodInfoMap.computeIfAbsent(userId, k -> new SonHaiReBloodInfo());
        info.setUserId(userId);
        info.addReBlood(reBlood);
        info.setTime(System.currentTimeMillis());
    }

    /**
     * 获取血量百分比
     * @return
     */
    public int getBloodRate() {
        long currentBlood = this.getBlood();
        long allBlood = this.getAllBlood();
        return BigDecimal.valueOf(currentBlood).divide(BigDecimal.valueOf(allBlood), 8, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100)).intValue();
    }

    /**
     * 检查CD中
     * @param userId
     * @return true:cd中，false:没cd中
     */
    public boolean checkHelpCd(long userId, int cd) {
        SonHaiReBloodInfo reBloodInfo = this.reBloodInfoMap.get(userId);
        if (reBloodInfo != null) {
            long cdEndTime = this.getHelpCdEndTime(userId, cd);
            if (cdEndTime >= System.currentTimeMillis()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取cd结束时间
     * @param userId
     * @return
     */
    public long getHelpCdEndTime(long userId, int cd) {
        long cdTime = cd * DateHelper.SECOND_MILLIONS;
        SonHaiReBloodInfo reBloodInfo = this.getReBloodInfoMap().get(userId);
        if (reBloodInfo != null) {
            return reBloodInfo.getTime() + cdTime;
        }
        return 0;
    }
}
